/**
 * 公共server(域名+端口)变量
 */
var server;
function addServer(server) {
    this.server = server;
}
/**
 * 登录和注册页面js提示
 */
function login() {
    $.ajax({
        url: server + "/login",
        method: "POST",
        data: {"usernameOrEmail": $('#usernameOrEmail').val(), "password": $('#password').val()},
        success: function (data) {
            if (data == "登陆成功") {
                window.location.href = server;
            }
            else {
                $('.login-register-tip').css("display", "block");
                $('.login-register-tip').text(data);
            }
        }
    })
}

//enter键自动提交表单，或者点击提交
$('.login-btn').click(function () {
    login();
})

function register() {
    $.ajax({
        url: server + "/register",
        method: "POST",
        data: {"username": $('#username').val(), "password": $('#password').val(), "email": $('#email').val()},
        success: function (data) {
            $('.login-register-tip').css("display", "block");
            $('.login-register-tip').text(data);
        }
    })
}
$('.btn-register').click(function () {
    register();
})

$(document).keydown(function (event) {
    if (event.keyCode == 13 & $('#password').length > 0)
        login();
    else if (event.keyCode == 13 & $('#tag').length > 0) {

    }
})

//作品详情页
function workOperate(data) {
    $.ajax({
        url: server + "/work/operate",
        method: "POST",
        data: {"wid": $('#wid').val(), "type": data},
        success: function (data) {
            if (data == "login_necessary") alert("请先登陆");
            else if (data == "success") window.location.reload();
            else alert("此功能不可用");
        }
    })
}

$('#recommend').click(function () {
    workOperate(this.id);
})
$('#collection').click(function () {
    workOperate(this.id);
})

$('#commit-comment').click(function () {
    $.ajax({
        url: server + "/work/addComment",
        data: {"info": $('#comment-content').val(), "wid": $('#wid').val()},
        method: "GET",
        success: function (data) {
            if (data == "login_necessary") alert("请登录");
            else if (data == "success") window.location.reload();
            else alert("评论失败");
        }
    })
})
$('.search-work').click(function () {
    window.location.href = server + "/work/show?key=" + $('#key').val();
})
//上传作品页面
var tags = new Array();
$(document).click(function () {
    $('#tag').val("");
})
// 添加标签子节点
$(document).keydown(function (event) {
    if (event.keyCode == 13 && $('#tag').length > 0 && $('#tag').val().length > 0) {
        var tag = checkTag($('#tag').val());

        if (!isExistInTags(tag)) {
            tags.push(tag);
            $('#tags').append("<label id='" + tag + "' class='label label-tag'>" + tag + "<a onclick='remove_tag(this.parentNode.id)' href='javascript:;' >&nbsp;<span class='glyphicon glyphicon-remove'></span></a> </label>");
        }

        $('#tag').val("");
    }
})
// 向服务器检查该tag
function checkTag(tag) {
    $.ajax({
        url: server + "/tag/check",
        method: "POST",
        async: false,
        data: {"tag": tag},
        success: function (data) {
            if (data != 'fail')
                tag = data;
        }
    })
    return tag;
}
//检查是否在tags中有该标签
function isExistInTags(tag) {
    for (var i = 0; i < tags.length; i++) {
        if (tags[i] == tag)
            return true;
    }
    return false;
}
// 移除标签子节点
function remove_tag(id) {
    $('#' + id).remove();
    var index = getArrayIndexByObject(id);
    tags.splice(index, 1);
}
//获取制定数组下标
function getArrayIndexByObject(obj) {
    for (var i = 0; i < tags.length; i++) {
        if (obj == tags[i])
            return i;
    }
}
//提交操作
function submit_work() {
    if ($('#title').val() == null || $('#content').val() == null || tags == null || tags.length == 0) {
        alert("输入项不能为空");
    } else {
        $.ajax({
            url: server + "/work/publish",
            data: {"title": $('#title').val(), "content": $('#content').val(), "tags": tags},
            dataType: "json",
            async: false,
            method: "POST",
            success: function (data) {
                window.location.href = server + "/work/show";
            },
            error: function (xmlhttp) {
                window.location.href = server + "/work/show";
            }
        })
    }
}
$(".submit-work").click(function () {
    submit_work()

})
// 作品展示页面js
$('#department').click(function () {
    $('#work-top-list').html("<li><a href=server+'/tag/技术部'>技术部</a></li>" +
        "<li><a href=server+'/tag/动漫部'>动漫部</a></li><li><a href=server+'/tag/技术部'>网络部</a></li>" +
        "<li><a href=server+'/tag/行政部'>行政部</a></li><li><a href=server+'/tag/项目部'>项目部</a></li>");
    $('.work-top-list').fadeIn();
})
$('#year').click(function () {
    $('#work-top-list').html("<li><a href='#'>2016</a></li>" +
        "<li><a href='#'>2017</a></li>");
    $('.work-top-list').fadeIn();
})

//个人设置页面
function timeoutModifyTip() {
    setTimeout("$('.modify-tip').css('display','none');", 4000);
}

$('.modify-base-info').click(function () {
    $.ajax({
        url: server + "/setting/modify/profile",
        method: "post",
        data: $('#base-info').serialize(),
        success: function (data) {
            if (data == "success")
                $('.modify-tip').text("修改成功");
            else
                $('.modify-tip').text(data);

            $('.modify-tip').css("display", "block");
            timeoutModifyTip();
        },
        error: function () {
            $('.modify-tip').text("修改失败");
            $('.modify-tip').css("display", "block");
            timeoutModifyTip();
        }
    })
})
$('.modify-pwd').click(function () {
    $.ajax({
        url: server + "/setting/modify/password",
        method: "post",
        data: $('#pwd').serialize(),
        success: function (data) {
            if (data == "success")
                $('.modify-tip').text("修改成功");
            else
                $('.modify-tip').text(data);

            $('.modify-tip').css("display", "block");
            $('.setting-pwd').val("");//清除原纪录
            timeoutModifyTip();
        },
        error: function () {
            $('.modify-tip').text("修改失败");
            $('.modify-tip').css("display", "block");
            $('.setting-pwd').val("");//清除原纪录
            timeoutModifyTip();
        }
    })
})
$('#upload-avatar-img').change(function () {
    var formDate = new FormData();
    formDate.append("file", $('#upload-avatar-img')[0].files[0]);

    $.ajax({
        url: "http://upload.zkdcloud.cn",
        data: formDate,
        cache: false,
        type: "POST",
        async: false,
        processData: false,
        contentType: false,
        success: function (data) {
            $('#avatar-img').attr("src", data);
            $('#modify-avatar-img').val(data);
        }
    })
})
$('.upload-avatar-btn').click(function () {
    $.ajax({
        url: server + "/setting/modify/avatar",
        type: "POST",
        data: {"avatarImg": $('#modify-avatar-img').val()},
        async: false,
        success: function (data) {
            $('.modify-tip').text("修改成功");
            $('.modify-tip').css("display", "block");
            timeoutModifyTip();
        },
        error: function (data) {
            $('.modify-tip').text(data);
            $('.modify-tip').css("display", "block");
            timeoutModifyTip();
        }
    })
})
$('#college').hover(function () {
    $.ajax({
        url: server + "/setting/collegeList",
        type: "POST",
        dataType: "JSON",
        async: false,
        success: function (data) {
            var c = $('#college').val();
            $('#college').empty();
            $.each(data, function (index, item) {
                if (c == item) {
                    $('#college').append("<option selected value='" + item + "'>" + item + "</option>");
                } else {
                    $('#college').append("<option value='" + item + "'>" + item + "</option>");
                }
            })
        },
        error: function (data) {
            $('.modify-tip').text(data);
            $('.modify-tip').css("display", "block");
            timeoutModifyTip();
        }
    })
})
$('#college').change(function () {
    majorFlash();
})
function majorFlash() {
    $.ajax({
        url: server + "/setting/majorList",
        type: "POST",
        data: {"college": $('#college').val()},
        dataType: "JSON",
        async: false,
        success: function (data) {
            var c = $('#major').val();
            $('#major').empty();
            $.each(data, function (index, item) {
                if (c == item) {
                    $('#major').append("<option selected value='" + item + "'>" + item + "</option>");
                } else {
                    $('#major').append("<option value='" + item + "'>" + item + "</option>");
                }
            })
        },
        error: function (data) {
            $('.modify-tip').text(data);
            $('.modify-tip').css("display", "block");
            timeoutModifyTip();
        }
    })
}

$('#classes').hover(function () {
    $.ajax({
        url: server + "/setting/classesList",
        type: "POST",
        dataType: "JSON",
        async: false,
        success: function (data) {
            var c = $('#classes').val();
            $('#classes').empty();
            $.each(data, function (index, item) {
                if (c == item) {
                    $('#classes').append("<option selected value='" + item + "'>" + item + "</option>");
                } else {
                    $('#classes').append("<option value='" + item + "'>" + item + "</option>");
                }
            })
        },
        error: function (data) {
            $('.modify-tip').text(data);
            $('.modify-tip').css("display", "block");
            timeoutModifyTip();
        }
    })
})
$('.btn-updateCD').click(function () {
    $.ajax({
        url: server + "/setting/updateCollegeDepartment",
        data: {"college": $('#college').val(), "major": $('#major').val(), "classes": $('#classes').val()},
        type: "POST",
        dataType: "JSON",
        success: function (data) {
            $('.modify-tip').text("修改成功");
            $('.modify-tip').css("display", "block");
            timeoutModifyTip();
        },
        error: function (xr) {
            $('.modify-tip').text(xr.responseText);
            $('.modify-tip').css("display", "block");
            timeoutModifyTip();
        }
    })
})


